<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;

class NianjiController extends AdminbaseController {
    public function index() {
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $Model = M();
        $query=$Model->query("SELECT count(id) as count FROM `app_nianji`   ");
        $count=$query[0]['count'];
        $page = $this->page($count, 20, $p);
        $nianjiData=$Model->query("SELECT * FROM `app_nianji`  limit ".(($p-1)*20).",20 ");
        
        $_nj=array();
        foreach ($nianjiData as $value){
            $_nj[]=$value['name'];
        }
        
        file_put_contents('./c/nj.txt', json_encode($_nj)); 
         
        $this->assign("page", $page->show('Admin'));
        $this->assign("nianjiData", $nianjiData);
         
        $this->display();
    }
  
    public function add() {
        
        $this->display();
    }
    
    public function add_post() {
        if (isset($_POST['name']) && !empty($_POST['name'])){
            $qu = M("nianji", 'app_');
            $data['name'] = addslashes(trim($_POST['name']));
            $qu->add($data);
            $info=json_encode(array('status'=>'success'));
            echo $info;
        }
    }
    
    
    
    public function edit() {
        if (isset($_GET['id'])){
            $id=$_GET['id'];
        }else{
            exit;
        }
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $Model = M();
        $njData=$Model->query("SELECT * FROM `app_nianji` where id='".$id."' limit 0,1  ");
        $this->assign("p", $p);
        $this->assign("njData", $njData[0]);
        $this->display();
    }
     
    public function editpost() {
        if (isset($_POST['name']) && isset($_POST['option_id'])){
            $qu = M("nianji", 'app_');
            $data['name'] = addslashes(trim($_POST['name']));
            $id=$_POST['option_id'];
            $qu->where('id='.$id)->save($data);
            $info=json_encode(array('status'=>'yes'));
            echo $info;
        }
    }
    
    public function nj_delete() {
        if (isset($_GET['id'])){
            $id=$_GET['id'];
            $qu = M("nianji", 'app_');
            $qu->where('id='.$id)->delete();
            $info=json_encode(array('state'=>'success','referer'=>'admin.php?g=Admin&m=Nianji&a=index'));
            echo $info;
        }
    }
 
}
